package com.example.oasystem.controller.dept;

import com.example.oasystem.dao.dept.AoaDeptDao;
import com.example.oasystem.entity.dept.AoaDept;
import com.example.oasystem.entity.page.Page;
import com.example.oasystem.entity.position.AoaPosition;
import com.example.oasystem.entity.user.AoaUser;
import com.example.oasystem.service.AoaDeptService;
import com.example.oasystem.service.AoaPositionService;
import com.example.oasystem.service.AoaUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class AoaDeptController {
    @Autowired
    private AoaUserService aoaUserService;
    @Autowired
    private AoaDeptDao aoaDeptDao;
    @Autowired
    private AoaDeptService aoaDeptService;
    @Autowired
    private AoaPositionService aoaPositionService;


    @RequestMapping("/deptmanage")
    public String deptManage(Model model) {
        List<AoaDept> aoaDeptList = aoaDeptService.selectAll();
        model.addAttribute("depts", aoaDeptList);
        return "user/deptmanage";
    }

    @RequestMapping("/deptedit")
    public String deptEdit(HttpServletRequest request, Model model) {
        String deptId = request.getParameter("dept");
        if (deptId != null) {
            AoaDept aoaDept = aoaDeptDao.selectById(Long.parseLong(deptId));
            model.addAttribute("dept", aoaDept);
        }
        return "user/deptedit";
    }

    @RequestMapping("/addDept")
    public String addDept(AoaDept aoaDept, HttpServletRequest request) {
        String xg = request.getParameter("xg");
        if ("add".equals(xg)) {
            aoaDeptDao.insert(aoaDept);
        } else {
            aoaDeptDao.updateById(aoaDept);
        }
        return "forward:deptmanage";
    }

    @RequestMapping("/readdept")
    public String readDept(@RequestParam(value = "page", required = false,defaultValue = "0")int page,HttpServletRequest request, Model model) {
        Long deptId = Long.parseLong(request.getParameter("deptid"));
        request.getSession().setAttribute("deptid",deptId);
        List<AoaUser> deptUsers = aoaUserService.selectByDeptId(deptId,null);
        int totalElement= deptUsers.size();
        int size=5;
        int totalPages=totalElement%size==0? totalElement/size:totalElement/size+1;
        boolean first;
        if (page==0)
        {
            first=true;
        }
        else
        {
            first=false;
        }
        boolean last;
        if (page==(totalPages-1))
        {
            last=true;
        }
        else
        {
            last=false;
        }
        Page p=new Page(totalElement,size,totalPages,page*size, first,last);
        System.out.println("p111111111111111111111111111111111111111111111111111111 = " + p);
        List<AoaUser> aoaUsers = aoaUserService.selectByDeptId(deptId, p);
        p=new Page(totalElement,size,totalPages,page, first,last);
        System.out.println("p2222222222222222222222222222222222222222222 = " + p);
        if (deptUsers.size() == 0) {
            aoaDeptDao.deleteById(deptId);
            return "forward:deptmanage";
        } else {

            AoaDept aoaDept = aoaDeptDao.selectById(deptId);
            System.out.println("aoaDept = " + aoaDept);
            AoaUser deptManager = aoaDeptService.selectDeptManagerById(deptId);
            List<AoaDept> depts = aoaDeptService.selectAll();
            List<AoaPosition> positions = aoaPositionService.selectByDeptId(deptId);

            model.addAttribute("page",p);
            model.addAttribute("dept", aoaDept);
            model.addAttribute("deptuser", aoaUsers);
            model.addAttribute("aoaUsers",deptUsers);
            model.addAttribute("deptmanage", deptManager);
            model.addAttribute("depts", depts);
            model.addAttribute("positions", positions);
            model.addAttribute("isread", true);
            model.addAttribute("url","deptpaging");
            return "user/deptread";
        }
    }

    @RequestMapping("/deptpaging")
    public String paging(@RequestParam(value = "page", required = false,defaultValue = "0") int page, HttpServletRequest request, Model model) {
        Long deptId = (Long) request.getSession().getAttribute("deptid");
        List<AoaUser> deptUsers = aoaUserService.selectByDeptId(deptId, null);
        int totalElement= deptUsers.size();
        int size=5;
        int totalPages=totalElement%size==0? totalElement/size:totalElement/size+1;
        boolean first;
        if (page==0)
        {
            first=true;
        }
        else
        {
            first=false;
        }
        boolean last;
        if (page==(totalPages-1))
        {
            last=true;
        }
        else
        {
            last=false;
        }
        Page p=new Page(totalElement,size,totalPages,page*size, first,last);
        List<AoaUser> aoaUsers = aoaUserService.selectByDeptId(deptId, p);
        p=new Page(totalElement,size,totalPages,page, first,last);
        if (deptUsers.size() == 0) {
            aoaDeptDao.deleteById(deptId);
            return "forward:deptmanage";
        } else {

            AoaDept aoaDept = aoaDeptDao.selectById(deptId);
            System.out.println("aoaDept = " + aoaDept);
            AoaUser deptManager = aoaDeptService.selectDeptManagerById(deptId);
            List<AoaDept> depts = aoaDeptService.selectAll();
            List<AoaPosition> positions = aoaPositionService.selectByDeptId(deptId);

            model.addAttribute("page", p);
            model.addAttribute("dept", aoaDept);
            model.addAttribute("deptuser", aoaUsers);
            model.addAttribute("aoaUsers",deptUsers);
            model.addAttribute("deptmanage", deptManager);
            model.addAttribute("depts", depts);
            model.addAttribute("positions", positions);
            model.addAttribute("isread", true);
            model.addAttribute("url", "deptpaging");
            return "user/deptmanagetable";
        }
    }
    @RequestMapping("/selectdept")
    @ResponseBody
    public List<AoaPosition> selectByDeptId(@RequestParam Long selectdeptid) {
        List<AoaPosition> positions = aoaPositionService.selectByDeptId(selectdeptid);
        return positions;
    }

    @RequestMapping("/deletdept")
    public String deleteDept(HttpServletRequest request) {
        Long deptId = Long.parseLong(request.getParameter("deletedeptid"));
        aoaDeptDao.deleteById(deptId);
        return "forward:deptmanage";
    }

    @RequestMapping("/deptandpositionchange")
    public String deptAndPositionChange(@RequestParam String deptid, @RequestParam String changedeptid, @RequestParam String positionid, @RequestParam String userid) {
        aoaUserService.updateDeptAndPosition(Long.parseLong(changedeptid), Long.parseLong(positionid), Long.parseLong(userid));
        return "forward:/readdept?deptid" + deptid;
    }

    @RequestMapping("/deptmanagerchange")
    public String deptManagerChange(@RequestParam String deptid, @RequestParam(value = "changedeptid", required = false, defaultValue = "") String changedeptid,
                                    @RequestParam(value = "positionid", required = false, defaultValue = "") String positionid, @RequestParam String newmanageid,
                                    @RequestParam(value = "oldmanageid", required = false, defaultValue = "") String oldmanageid) {
        /*System.out.println("deptid = " + deptid);
        System.out.println("changedeptid = " + changedeptid);
        System.out.println("positionid = " + positionid);
        System.out.println("oldmanageid = " + oldmanageid);
        System.out.println("newmanageid = " + newmanageid);*/
        if(!"".equals(changedeptid) && !"".equals(positionid)&&!"".equals(oldmanageid)) {
            aoaUserService.updateDeptAndPosition(Long.parseLong(changedeptid), Long.parseLong(positionid), Long.parseLong(oldmanageid));
        }
        if (!"".equals(newmanageid) && newmanageid != null) {
            aoaUserService.updatePositionId(Long.parseLong(newmanageid), Long.parseLong(deptid));
        }
        return "forward:/readdept?deptid" + deptid;
    }
}
